package login_register

import (
	"errors"
	"internal/api/auth"
	"internal/api/types"
	"internal/store"
	"internal/store/mysql"

	"internal/util"
)

func AccountLogin(req *types.LoginReq, resp *types.LoginResp) error {
	f := &store.FkUser{
		UserName: req.Username,
	}
	err := mysql.GetConn().First(f, "f_name=?", f.UserName).Error
	if err != nil {
		util.LogSrv.Error("f_name error: %v", err)
		return errors.New("account not found")
	}
	if !CheckPassword(req.Password, f.Password) {
		return errors.New("account password error")
	}
	resp.AccessToken, err = auth.GenerateToken(f.UserId, req.IsAdmin)
	return nil
}
